package org.quickjsf.core.base.controller;

import javax.faces.event.ActionEvent;

import org.primefaces.model.MenuModel;
import org.quickjsf.core.base.controller.support.EditableDTPanelSupport;
import org.quickjsf.core.base.controller.support.FilterableDTPanelSupport;
import org.quickjsf.core.base.viewmodel.EditableAndFilterableViewModel;
import org.quickjsf.core.base.viewmodel.support.EditableViewModelSupport;
import org.quickjsf.core.base.viewmodel.support.FilterableViewModelSupport;

public abstract class EditableAndFilterableDTPanelController<T> extends BaseDTPanelController<T> implements IEditable<T>, IFilterable<T> {
	
	private EditableDTPanelSupport<T> editableDTPanelSupport;
	private FilterableDTPanelSupport<T> filterableDTPanelSupport;
	
	public EditableAndFilterableDTPanelController(EditableAndFilterableViewModel<T> viewModel) throws Exception {
		super(viewModel);
		initEditableDTPanelSupport(viewModel.getEditableViewModel());
		initFilterableDTPanelSupport(viewModel.getFilterViewModel());
	}
	
	private void initEditableDTPanelSupport(EditableViewModelSupport<T> viewModel) {
		editableDTPanelSupport = new EditableDTPanelSupport<T>(viewModel){
			@Override
			public BaseDTPanelController<T> getBaseDTPanelController() {
				return EditableAndFilterableDTPanelController.this;
			}
		};
	}
	
	private void initFilterableDTPanelSupport(FilterableViewModelSupport<T> filterViewModel) {
		filterableDTPanelSupport = new FilterableDTPanelSupport<T>(filterViewModel){
			@Override
			public BaseDTPanelController<T> getBaseDTPanelController() {
				return EditableAndFilterableDTPanelController.this;
			}
		};
	}
	
//	@Override
//	public boolean onAddFilterComponent(OutputPanel filterValuePanel, String field) {
//		return false;
//	}
	
	@Override
	public void addMenuBarItem(MenuModel menuModel) {
		super.addMenuBarItem(menuModel);
		editableDTPanelSupport.addEditableMenuModel(menuModel);
	}
	
	public void save(ActionEvent actionEvent) throws Exception {
		editableDTPanelSupport.save(this);
	}
	
	public void delete(ActionEvent actionEvent) {
		editableDTPanelSupport.delete(this);
	}
	
	public void updateFilterComponentOnPanel() {
		filterableDTPanelSupport.updateComponentOnPanel();
	}
	
	public final void doFilter(ActionEvent event) {
		filterableDTPanelSupport.searchFilter(this);
	}
	/**********************************************empty implement*********************************/
	@Override
	public void beforeFilter() {}
	@Override
	public void afterFilter() {}
	
	public EditableDTPanelSupport<T> getEditableDTPanelSupport() {
		return editableDTPanelSupport;
	}

	public void setEditableDTPanelSupport(
			EditableDTPanelSupport<T> editableDTPanelSupport) {
		this.editableDTPanelSupport = editableDTPanelSupport;
	}

	public FilterableDTPanelSupport<T> getFilterableDTPanelSupport() {
		return filterableDTPanelSupport;
	}

	public void setFilterableDTPanelSupport(
			FilterableDTPanelSupport<T> filterableDTPanelSupport) {
		this.filterableDTPanelSupport = filterableDTPanelSupport;
	}
}
